package cn.com.yiqi.wsc.config.credential;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * @author 何刚
 * @ClassName: ${class}
 * @Description: TODO
 * @date 2017/2/28
 */
public class PasswordCredentialsMatchers extends SimpleCredentialsMatcher {


    @Resource(name = "encoder")
    private BCryptPasswordEncoder encoder;

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

        String usernameDB = info.getPrincipals().toString();
        String passwordDB = info.getCredentials().toString();
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String usernameInput = usernamePasswordToken.getUsername();
        String passwordInput = new String(usernamePasswordToken.getPassword());

        return usernameInput.equals(usernameDB) && encoder.matches(passwordInput, passwordDB);
    }
}
